package com.engine.query.cmd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.interceptor.JdbcCommandContext;
import com.engine.core.interceptor.ResponseNextCommand;
import com.engine.core.rowMapper.StructBuildBiz;
import com.engine.core.util.QueryUtil;
import com.engine.core.util.Response;
import com.engine.core.util.Utils;
import com.engine.system.User;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据结构化处理
 */
public class AssociationCommand extends ResponseNextCommand {

    private int associationId;
    private String associationContent;

    /**
     * @param user
     * @param params
     */
    public AssociationCommand(User user, Map<String, Object> params) {
        super(user, params);
        if(params.containsKey(QueryUtil.QUERY_ASSOCIATION_ID)){
            associationId= Utils.getInt((String)params.get(QueryUtil.QUERY_ASSOCIATION_ID),-1);
        }
        if(associationId<1&&params.containsKey(QueryUtil.QUERY_CONTENT)){
            if(JSON.isValidObject((String)params.get(QueryUtil.QUERY_CONTENT))){
                JSONObject contentJSON=JSON.parseObject((String)params.get(QueryUtil.QUERY_CONTENT));
                if (contentJSON.containsKey(QueryUtil.QUERY_ASSOCIATION_ID)) {
                    associationId=Utils.getInt(contentJSON.getString(QueryUtil.QUERY_ASSOCIATION_ID),-1);
                }
                if(contentJSON.containsKey(QueryUtil.QUERY_ASSOCIATION)){
                    associationContent=contentJSON.getString(QueryUtil.QUERY_ASSOCIATION);
                }
            }
        }
    }

    /**
     * @param lastResponse
     * @param commandContext
     * @return
     * @throws Exception
     */
    @Override
    public Response execute(Response lastResponse, JdbcCommandContext commandContext) throws Exception {
         if(!lastResponse.isSuccess()){
             return lastResponse;
         }
         if(associationId<1 && StringUtils.isBlank(associationContent)){
             return lastResponse;
         }
         Object resData=lastResponse.data();
        try {
            List<Map<String,Object>> dataList=new ArrayList<>();
            boolean isMap=false;
            if(Map.class.isInstance(resData)){
                dataList.add((Map<String,Object>)resData);
                isMap=true;
            }else{
                dataList=(List<Map<String,Object>>)resData;
                isMap=false;
            }
            List<StructBuildBiz.Association> associationList=JSONObject.parseArray(associationContent,StructBuildBiz.Association.class);
            List<Map<String,Object>>  returnData=StructBuildBiz.buildDataStruct(dataList,associationList);
            if(isMap){
                return Response.static_success("转化成功!").data(returnData.get(0));
            }else{
                return Response.static_success("转化成功!").data(dataList);
            }
        } catch (Exception e) {
            return Response.static_fail(500).setErr(e);
        }
    }
}
